package org.jiangnantaste.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jiangnantaste.bo.DeliveryOrderStatusBo;
import org.jiangnantaste.domain.DeliveryOrder;
import org.jiangnantaste.util.HttpUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class ListAllDeliveryOrderStatusBoFinishedActivity extends ListActivity{
	private ListView myView;
	private MyAdapter adapter;
	List<DeliveryOrderStatusBo> deliveryorderstatusbolist;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		try {
			setTitle("江南味道");
			
			SharedPreferences pres = getSharedPreferences("user_msg",
					MODE_WORLD_READABLE);
			String userid = pres.getString("userid", "");

			String url = HttpUtil.BASE_URL+"jsoncontroller/listAllDeliveryOrderStatusBoFinishedController";
			url = url + "?employeeid="+userid;
			String result = HttpUtil.queryStringForPost(url);
	
			deliveryorderstatusbolist = parseDeliveryOrderStatusBoList(result);
			adapter = new MyAdapter(this);
			adapter.notifyDataSetInvalidated();
			myView = new ListView(this);
			myView.setAdapter(adapter);
			setContentView(myView);
		} catch(Exception e){			
		}		
	}
	
	@Override
	protected void onResume() { 
		super.onResume();
		try {
			SharedPreferences pres = getSharedPreferences("user_msg",
					MODE_WORLD_READABLE);
			String userid = pres.getString("userid", "");
			
			String url = HttpUtil.BASE_URL+"jsoncontroller/listAllDeliveryOrderStatusBoFinishedController";
			url = url + "?employeeid="+userid;
			String result = HttpUtil.queryStringForPost(url);
	
			deliveryorderstatusbolist = parseDeliveryOrderStatusBoList(result);
			adapter.notifyDataSetInvalidated();
		} catch (Exception e){
			
		}
	};

	 private List<Map<String, Object>> getData(List<DeliveryOrderStatusBo> originalList) {
	        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	        
	        for (int i=0; i < originalList.size(); i++){
		        Map<String, Object> map = new HashMap<String, Object>();
	        	map.put("派送单序号", originalList.get(i).getDeliveryordercode());
	        	map.put("联系人", originalList.get(i).getContactperson());
	        	map.put("状态", originalList.get(i).getStatus());
	        	list.add(map);
	        }
	        return list;
	    }
	
	public List<DeliveryOrderStatusBo> parseDeliveryOrderStatusBoList(String json) throws Exception{
		ArrayList<DeliveryOrderStatusBo> list = new ArrayList<DeliveryOrderStatusBo>();
		JSONTokener jsonParser = new JSONTokener(json);
		JSONArray arr = (JSONArray) jsonParser.nextValue(); 
		for (int i = 0; i< arr.length(); i++){
			DeliveryOrderStatusBo od = new DeliveryOrderStatusBo();
			JSONObject jo = arr.getJSONObject(i);
			od.setAddress(jo.getString("address"));
			od.setBoxnumber(jo.getInt("boxnumber"));
			od.setContactperson(jo.getString("contactperson"));
			od.setDeliveryordercode(jo.getString("deliveryordercode"));
			od.setDeliveryorderid(jo.getLong("deliveryorderid"));
			od.setMobile(jo.getString("mobile"));
			od.setPhone(jo.getString("phone"));
			od.setStatus(jo.getString("status"));
			od.setEmployeeid(jo.getLong("employeeid"));
			od.setEmployeename(jo.getString("employeename"));
			od.setClientid(jo.getLong("clientid"));
			od.setClientname(jo.getString("clientname"));
			od.setSequence(jo.getInt("sequence"));
			list.add(od);
		}
		return list;
	}

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {         
        Log.v("JerryDebug::ListViewClick::", (String)deliveryorderstatusbolist.get(position).getContactperson());
    }

    public void showInfo(int position){
        new AlertDialog.Builder(this)
        .setTitle("江南味道")
        .setMessage(deliveryorderstatusbolist.get(position).toString())
        .setPositiveButton("", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        })
        .show();
         
    }
     
     
     
    public final class ViewHolder{
        public TextView deliveryordercode;
        public TextView deliveryordercontactperson;
        public TextView deliveryorderstatus;
        public Button viewBtn;
    }
     
     
    public class MyAdapter extends BaseAdapter{
 
        private LayoutInflater mInflater;
         
         
        public MyAdapter(Context context){
            this.mInflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            return deliveryorderstatusbolist.size();
        }
 
        @Override
        public Object getItem(int position) {
            return deliveryorderstatusbolist.get(position);
        }
 
        @Override
        public long getItemId(int position) {
        	return deliveryorderstatusbolist.get(position).getDeliveryorderid();
        }
 
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
             
            ViewHolder holder = null;
            if (convertView == null) {
                 
                holder=new ViewHolder();  
                 
                convertView = mInflater.inflate(R.layout.deliveryorder_list_noaction, null);
                holder.deliveryordercode = (TextView)convertView.findViewById(R.id.deliveryordercode);
                holder.deliveryordercontactperson = (TextView)convertView.findViewById(R.id.deliveryordercontactperson);
                holder.deliveryorderstatus = (TextView)convertView.findViewById(R.id.deliveryorderstatus);
                holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn_noaction);
                convertView.setTag(holder);
            }else {
                 
                holder = (ViewHolder)convertView.getTag();
            }
             
             
            holder.deliveryordercode.setText((String)deliveryorderstatusbolist.get(position).getDeliveryordercode());
            holder.deliveryordercontactperson.setText((String)deliveryorderstatusbolist.get(position).getContactperson());
            holder.deliveryorderstatus.setText((String)deliveryorderstatusbolist.get(position).getStatus());
            holder.viewBtn.setOnClickListener(new View.OnClickListener() {
                 
                @Override
                public void onClick(View v) {
                    showInfo(position);                 
                }
            });
             
             
            return convertView;
        }
         
    }
}
